﻿using System;
using System.Data;
using System.Windows.Forms;
using 售后客户管理.Common;

namespace 售后客户管理.Forms
{

    public partial class FmSelectionsMsg : Form
    {
        public FmSelectionsMsg()
        {
            InitializeComponent();
        }
        private Common.SqlConnectionHelper SqlConnectionHelper = new Common.SqlConnectionHelper(Program.configs,ConfigCode.Main);
        private void FmSelected_Load(object sender, EventArgs e)
        {
            this.dataGridView1.BackgroundColor = Common.MyColor.NewColor_Lavender;
            this.btnAction.Paint += DrawingImage.RoundButton_Paint;
            this.btnAction.BackColor = Common.MyColor.NewColor_Lavender;
            this.btnAction.Top = (this.btnAction.Parent.Height - this.btnAction.Width) / 2;
            this.cbbSelectType.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("selectType", SqlConnectionHelper.sqlConnection);
            this.cbbSelectType.SelectedIndex = 0;
            this.dataGridView1.DoubleBuffered(true);
            ModelLibrary.Setting setting = new ModelLibrary.Setting() { SName = "提前天数", };
            this.fcTiQianTianShu.Text = DrawingImage.DataTableToObject<ModelLibrary.Setting>(SqlClassLibrary.SqlGetSet.GetSettingDataTable(setting, SqlConnectionHelper.sqlConnection), new ModelLibrary.Setting()).NowValue;
        }

        private void BtnAction_Click(object sender, EventArgs e)
        {
            this.dataGridView1.VirtualMode = true;
            DataTable dataTable = SqlClassLibrary.SqlGetSet.GetSelectToDataTable(SqlConnectionHelper.sqlConnection, this.cbbSelectType.Text.Trim(), Convert.ToInt32(this.fcTiQianTianShu.Text));
            this.dataGridView1.DataSource = dataTable;
            DataGridViewColumn dvgcol = new DataGridViewColumn();
            for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
            {
                this.dataGridView1.Columns[i].DataPropertyName = this.dataGridView1.Columns[i].Name;
            }
            DrawingImage.ReNameDataGridView(this.dataGridView1,SqlConnectionHelper.sqlConnection);
            //this.dataGridView1.Visible = true;
            this.label2.Text = "共  " + dataTable.Rows.Count.ToString() + "  行";
            this.label3.Text = "金额合计：" + dataTable.Compute("sum(hetje)", "true").ToString();
        }

        private void DataGridView1_DoubleClick(object sender, EventArgs e)
        {
            DataGridViewRow dgvr = this.dataGridView1.SelectedRows[0];
            ModelLibrary.Customer customer = new ModelLibrary.Customer() { Custid = dgvr.Cells["custid"].Value.ToString() };
            DataTable dataTable = SqlClassLibrary.SqlGetSet.GetCustomerDT(customer, SqlConnectionHelper.sqlConnection);
            customer = DrawingImage.DataTableToObject<ModelLibrary.Customer>(dataTable, customer);

            FmMain fmMain = (FmMain)this.Parent.Parent.Parent.Parent.Parent;
            fmMain.Customer = customer;
            Button btn = new Button() { Text = "客户资料" };
            fmMain.Btn_Click(btn, new EventArgs());

        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = "";
            SaveFileDialog saveFile = new SaveFileDialog
            {
                Filter = "2010Excel文件(*.xlsx)|*.xlsx",
                FilterIndex = 1,
                Title = "保存EXCEL为"
            };
            if (saveFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            filePath = saveFile.FileName;
            if (ExcelHelper.OutputExcelFile(DrawingImage.DataGridViewToDataTable(this.dataGridView1, ModelLibrary.ColumnType.HeaderText, this.cbbSelectType.Text,true), filePath))
            {
                if (MessageBox.Show("保存成功！\n是否需要打开此文件？？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    System.Diagnostics.Process.Start(filePath);
                }
                return;
            }
            MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void FbcFilter_DoubleClick(object sender, EventArgs e)
        {
            string inserted = this.fbcFilter.Text.Trim();
            if (inserted == "" || inserted == null)
            {
                MessageBox.Show("没有输入值，请重新录入！", "提示", MessageBoxButtons.OK
                    , MessageBoxIcon.Error);
                return;
            }
            this.dataGridView1.DataSource = SqlClassLibrary.SqlGetSet.GetCustomerDT(new ModelLibrary.Customer() { Pym = inserted, Custname = inserted, Custbh = inserted, }, SqlConnectionHelper.sqlConnection);
        }

        private void FbcFilter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                FbcFilter_DoubleClick(sender, new EventArgs());
            }
        }

        private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
        {
            DrawingImage.ReNameDataGridView(this.dataGridView1,SqlConnectionHelper.sqlConnection);
            if (this.dataGridView1.DataSource.GetType() == typeof(DataTable))
            {
                DataTable dataTable = (DataTable)(this.dataGridView1.DataSource);
                this.label2.Text = "共  " + dataTable.Rows.Count.ToString() + "  行";

                this.label3.Text = "金额合计：" + (dataTable.Columns.Contains("hetje") ? dataTable.Compute("sum(hetje)", "true").ToString() : "0.00");
            }
        }
    }
}
